package fons.navigator.ui.actions;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import javax.swing.AbstractAction;

import fons.navigator.ui.NavigatorMainFrame;

public abstract class NavigatorAbstractAction extends AbstractAction implements PropertyChangeListener {
	private static final long serialVersionUID = 1L;
	private final NavigatorMainFrame app;
	private boolean enabledWithoutContext;
	
	public NavigatorAbstractAction (NavigatorMainFrame app, boolean enabledWithoutContext) {
		this.app = app;
		this.setEnabled(enabledWithoutContext);
		this.enabledWithoutContext = enabledWithoutContext;
		app.addPropertyChangeListener(this);
	}
	
	@Override
	public void propertyChange(PropertyChangeEvent evt) {
		if (!enabledWithoutContext
			&& NavigatorMainFrame.CONTEXT_CHANGED.equals(evt.getPropertyName())) {
			
			if (app.getActiveContext() != null) {
				this.setEnabled(true);
			} else {
				this.setEnabled(false);
			}
		}
	}
	
	protected NavigatorMainFrame getApp() {
		return app;
	}
}
